﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraPivotGrid;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Controls;
using efuture.utils;

namespace efuture.vdrp
{
    public partial class frmExpressionEditor : DevExpress.XtraEditors.XtraForm
    {
        public frmExpressionEditor()
        {
            InitializeComponent();

            ImageComboBoxItem item = null;

            item = new ImageComboBoxItem("数字", DevExpress.Data.UnboundColumnType.Decimal.ToString());
            this.imageComboBoxEdit1.Properties.Items.Add(item);

            item = new ImageComboBoxItem("日期", DevExpress.Data.UnboundColumnType.DateTime.ToString());
            this.imageComboBoxEdit1.Properties.Items.Add(item);

            item = new ImageComboBoxItem("字符", DevExpress.Data.UnboundColumnType.String.ToString());
            this.imageComboBoxEdit1.Properties.Items.Add(item);

            this.imageComboBoxEdit1.SelectedIndex = 0;
        }

        GridControl grid;
        public GridControl Grid
        {
            get { return grid; }
            set { grid = value; }
        }

        PivotGridControl pivotGrid;
        public PivotGridControl PivotGrid
        {
            get { return pivotGrid; }
            set { pivotGrid = value; }
        }

        PivotGridField GetNewInvisibleBonusField()
        {
            ImageComboBoxItem item = (ImageComboBoxItem)this.imageComboBoxEdit1.SelectedItem;
            Type t = typeof(DevExpress.Data.UnboundColumnType);

            PivotGridField newBonusField = new PivotGridField(teBonusName.Text, PivotArea.DataArea);
            newBonusField.Caption = teBonusName.Text;
            newBonusField.Name = "field_" + VdrpUtils.getIdentity();
            newBonusField.FieldName = "field_" + VdrpUtils.getIdentity();
            newBonusField.Visible = false;
            newBonusField.UnboundType = (DevExpress.Data.UnboundColumnType)Enum.Parse(t, item.Value.ToString());
            newBonusField.UnboundExpression = beExpression.Text;
            newBonusField.Options.ShowUnboundExpressionMenu = true;
            return newBonusField;
        }

        GridColumn GetNewInvisibleBonusColumn()
        {
            ImageComboBoxItem item = (ImageComboBoxItem)this.imageComboBoxEdit1.SelectedItem;
            Type t = typeof(DevExpress.Data.UnboundColumnType);

            GridColumn newBonusField = new GridColumn();
            newBonusField.Caption = teBonusName.Text;
            newBonusField.Name = "field_" + VdrpUtils.getIdentity();
            newBonusField.FieldName = "field_" + VdrpUtils.getIdentity();
            newBonusField.OptionsColumn.AllowEdit = false;
            newBonusField.Visible = false;
            newBonusField.UnboundType = (DevExpress.Data.UnboundColumnType)Enum.Parse(t, item.Value.ToString());
            newBonusField.UnboundExpression = beExpression.Text;
            newBonusField.ShowUnboundExpressionMenu = true;
            return newBonusField;
        }

        private void beExpression_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (PivotGrid != null)
            {
                PivotGridField newBonus = GetNewInvisibleBonusField();
                PivotGrid.Fields.Add(newBonus);
                PivotGrid.ShowUnboundExpressionEditor(newBonus);
                beExpression.Text = newBonus.UnboundExpression;
                PivotGrid.Fields.Remove(newBonus);
            }
            else if (Grid != null)
            {
                GridColumn newBonus = GetNewInvisibleBonusColumn();
                (Grid.MainView as GridView).Columns.Add(newBonus);
                (Grid.MainView as GridView).ShowUnboundExpressionEditor(newBonus);
                beExpression.Text = newBonus.UnboundExpression;
                (Grid.MainView as GridView).Columns.Remove(newBonus);
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (PivotGrid != null)
            {
                PivotGridField field = GetNewInvisibleBonusField();
                field.Visible = true;
                field.AreaIndex = 0;// PivotGrid.Fields["BonusAmount"].AreaIndex;
                PivotGrid.Fields.Add(field);
                teBonusName.Text = string.Empty;
                buttonOK.Enabled = false;
                beExpression.Text = string.Empty;
                beExpression.Enabled = false;
                //PivotGrid.BestFit();
            }
            else if (Grid != null)
            {
                GridColumn field = GetNewInvisibleBonusColumn();
                field.Visible = true;
                field.VisibleIndex = (Grid.MainView as GridView).VisibleColumns.Count;
                (Grid.MainView as GridView).Columns.Add(field);
                teBonusName.Text = string.Empty;
                buttonOK.Enabled = false;
                beExpression.Text = string.Empty;
                beExpression.Enabled = false;
            }

            DialogResult = DialogResult.OK;
        }

        private void teBonusName_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            if (string.IsNullOrEmpty(teBonusName.Text))
            {
                buttonOK.Enabled = false;
                beExpression.Enabled = false;
            }
            else
            {
                buttonOK.Enabled = true;
                beExpression.Enabled = true;
            }
        }
    }
}